 var shareApi = 'https://api.w293.cn/'
var jyhsApi = 'https://api.jyhs.ls293.cn/'
var mealApi = 'https://api.meal.w293.cn/'
 module.exports = {
    getShareApi : getShareApi,
    getJyhsApi: getJyhsApi,
    getMealApi: getMealApi,
    modal: modal,
    empty: empty,
    emptyPropertyObj: emptyPropertyObj,
    isArray: isArray,
    isObject: isObject,
    addValueForKey: addValueForKey,
    getLoc: getLoc,
    shareApi: shareApi,
    getFormatDate: getFormatDate,
    confirm: confirm,
    Modal: Modal,
    getIndexInList: getIndexInList,
    Toast: Toast,
    toast: toast,
    toastAllText: toastAllText,
    addMoneyRecord_User: addMoneyRecord_User,
    round: round,
    roundStr: roundStr,
    countSubString: countSubString,
    filterStr: filterStr,
 }
//获取旧约回收接口
function getJyhsApi(urlTail, param, callback) {
   var apiUrl = jyhsApi + urlTail
   request(apiUrl, param, callback)
}
 //获取爱共享接口1
 function getShareApi(urlTail, param, callback) {
    var apiUrl = shareApi + urlTail
    request(apiUrl, param, callback)
 }
//获取爱共享接口2
function getMealApi(urlTail, param, callback) {
   var apiUrl = mealApi + urlTail
   request(apiUrl, param, callback)
}
 //调用微信获取数据接口
 function request(apiUrl, param, callback) {
    wx.request({
       url: apiUrl,
       data: param,
       method: 'POST',
       success: function(res) {
          console.log('【地址】 ' + apiUrl, '\n【参数】', param, '\n【成功】', res);
          var d = res.data
          if (d == '') return
          if (typeof(callback) == 'function') {
             callback(d)
          }
       },
       fail: function(res) {
          toast('网络请求失败！')
          console.log('【地址】 ' + apiUrl, '\n【参数】', param, '\n【失败】', res);
       },
       complete: function(res) {
          if (res.statusCode != 200 && apiUrl != 'https://api.w293.cn/report/Rmg/addFailLog') {
             var params = {
                Api_Url: apiUrl,
                Api_Param: JSON.stringify(param),
                Report_Content: res.data,
                Status_Code: res.statusCode
             }
             //如果有User_ID或Biz_ID的参数，则传
             for (var key in param) {
                if (key.indexOf('User_ID') != -1 && empty(params.User_ID)) {
                   params.User_ID = param[key]
                } else if (key.indexOf('Biz_ID') != -1 && empty(params.Biz_ID)) {
                   params.Biz_ID = param[key]
                }
             }
             getShareApi('report/Rmg/addFailLog', params)
          }
       },
    })
 }

 //提示
 function Toast(msg) {
    wx.showToast({
       title: msg
    })
 }

 function toastAllText(msg) {
    wx.showToast({
       title: msg,
       icon: 'none'
    })
 }

 //带图标提示
 function toast(msg) {
    wx.showToast({
       title: msg,
       image: '/resource/images/lengtu.jpg',
    })
 }

 //不带标题提示
 function Modal(text) {
    wx.showModal({
       content: text,
       cancelText: '',
       confirmText: 'OK',
    })
 }

 //带标题提示
 function modal(msg, title) {
    wx.showModal({
       title: title ? title : '温馨提示',
       content: msg,
    })
 }

 //提示框
 function confirm(title, text, cancelText, confirmText, callback) {
    var param = {
       content: text,
       showCancel: !empty(cancelText),
       cancelText: cancelText,
       confirmText: confirmText,
       success: function(res) {
          console.log('confirm:success:', res)
          if (callback)
             callback(res.confirm)
       },
       fail: function(res) {},
    }
    if (!empty(title)) {
       param.title = title
    }
    wx.showModal(param)
 }

 // 判断是否为空
 function empty(val) {
    if (val == undefined || val == null || val === '' || val == 'undefined') {
       return true;
    }

    if (isArray(val)) {
       if (val.length == 0) {
          return true
       }
    }

    if (typeof(val) == 'number' && isNaN(val)) {
       return true;
    }

    return false;
 }

 //判断是否为空属性对象
 function emptyPropertyObj(val) {
    if (isObject(val)) {
       if (JSON.stringify(val) == '{}') {
          return true;
       }
    }
    return false;
 }

 //增加key和value
 function addValueForKey(toDict, fromDict, keyArray = []) {
    if (!isArray(keyArray)) {
       toast('第3个传参类型错误')
       return toDict
    }
    var len = keyArray.length
    if (len == 0) {
       for (var key in fromDict) {
          toDict[key] = fromDict[key]
       }
    } else {
       for (var i = 0; i < len; i++) {
          key = keyArray[i]

          if (fromDict[key]) {
             console.log('有key[' + i + ']==' + key)
             toDict[key] = fromDict[key]
          } else {
             console.log('没key[' + i + ']==' + key)
          }
       }
    }
    return toDict
 }

 //查对象在对象数组中的下标，不存在则返回-1，keyArray是要求比对的key
 function getIndexInList(obj, list, keyArray = []) {
    var count = list.length
    if (count == 0 || !isArray(list) || empty(list))
       return -1
    for (var i = 0; i < count; i++) {
       if (twoObjIsSame(obj, list[i], keyArray)) {
          return i
       }
    }
    return -1
 }

 //判断两对象是否相同
 function twoObjIsSame(obj, _obj, keyArray = []) {
    var len = keyArray.length
    if (len == 0) {
       for (var key in obj) {
          if (obj[key] != _obj[key]) {
             console.log('1obj.' + key + '==' + obj[key] + ',1_obj.' + key + '== ' + _obj[key])
             return 0
          }
       }
    } else {
       for (var i = 0; i < len; i++) {
          key = keyArray[i]
          if (obj[key] != _obj[key]) {
             console.log('2obj.' + key + '==' + obj[key].Sku_Name + ',2_obj.' + key + '== ' + _obj[key].Sku_Name)
             return 0
          }
       }
    }
    return 1
 }

 //是否数组
 function isArray(obj) {
    var res = Object.prototype.toString.call(obj)
    return res == '[object Array]'
 }

 //是否对象
 function isObject(obj) {
    var res = Object.prototype.toString.call(obj)
    return res == '[object Object]'
 }

 //从对象获取经纬度
 function getLoc(obj) {
    return {
       latitude: obj.latitude,
       longitude: obj.longitude
    }
 }

 function getFormatDate(type, time) {
    //type==1,只要日期 
    //type==2,要日期时间
    //type==3,时间戳转天数
    var date = time ? new Date(time) : new Date();

    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();

    var currentDate = ''

    if (type == 1 || type == 2) {
       if (month >= 1 && month <= 9) {
          month = "0" + month;
       }
       if (strDate >= 0 && strDate <= 9) {
          strDate = "0" + strDate;
       }
       currentDate = date.getFullYear() + seperator1 + month + seperator1 + strDate
    }
    if (type == 2) {
       currentDate += " " + date.getHours() + seperator2 + date.getMinutes() +
          seperator2 + date.getSeconds();
    }
    if (type == 3) {
       currentDate = parseInt(time / (1000 * 60 * 60 * 24))
    }

    return currentDate;
 }



 //添加资金流水记录(用户)
 function addMoneyRecord_User(param, callback) {
    this.getShareApi('money/Record/userAdd', param, function(res) {
       if (typeof(callback) === 'function') {
          callback(res)
       }
    })
 }


 //小数四舍五入
 function round(val, num) {
    var n = Math.pow(10, num)
    return Math.round(val * n) / n
 }

 //小数四舍五入(返回字符串格式)
 // '1.23' '5.00' '0.50'
 function roundStr(val, num) {
    var num_str = round(val, num).toString()
    var x_num_str = num_str.split('.')[1]
    var count = x_num_str ? num - x_num_str.length : num
    num_str = x_num_str ? num_str : (num_str + '.')
    for (; count--; num_str += '0');
    return num_str
 }

 //统计字符串中包含给定的子字符串的个数
 function countSubString(str, subStr) {
    var n = 0
    if (str && subStr) {
       n = (str.split(subStr)).length - 1
    }
    return n
 }

 //过滤字符串
 function filterStr(str, flag) {
    var result = ''
    var n = 0;
    if (str && flag) {
       result = str.split('').map(char => {
          if (char === flag) {
             n++
          }
          return char === flag && n > 1 ? '' : char
       }).join('')
    }
    return result
 }